<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Order_course_auth extends MY_Controller {
	function __construct(){
			parent::__construct();
			$this->load->model('Order_course_auths');
		}

	function school_order_records($record_id = 0, $data_type = 1){
		$this->load->model("Topics");
		$result = $this->Order_course_auths->get_order_record_codes($record_id, $data_type);
		$this->smarty->assign("codes", $result);
		$this->smarty->display("order_course_auth/school_order_records.html");
	}

	function send_school_order_records($record_id = 0, $data_type = 1, $order_num){
		$this->load->model("Topics");
		$result = $this->Order_course_auths->get_order_record_codes($record_id, $data_type, 1);
		$this->smarty->assign("codes", $result);
		$this->smarty->assign("data_type", $data_type);
		$this->smarty->assign("order_num", $order_num);
		$this->smarty->display("order_course_auth/send_school_order_records.html");
	}

	function send($code = 0, $data_type = 0, $order_num = 0){
		if(empty($code)){
			$result['error'] = 1;
			$result['msg'] = '请选择一个有效的激活码';
			echo json_encode($result);exit;
		}
		if(empty($data_type)){
			$result['error'] = 1;
			$result['msg'] = '激活类型错误';
			echo json_encode($result);exit;
		}
		if(empty($order_num)){
			$result['error'] = 1;
			$result['msg'] = '请选择一个需要方法激活码的订单';
			echo json_encode($result);exit;
		}

		if($data_type > 0 && $data_type < 4){
			$code_info = get_field_by_where("o2o_school_system_video_course_codes", "*", "code='{$code}'");
		}elseif ($data_type == 4) {
			$code_info = get_field_by_where("o2o_school_system_topic_codes", "*", "code='{$code}'");
		}elseif ($data_type == 5) {
			$code_info = get_field_by_where("o2o_school_system_shixun_codes", "*", "code='{$code}'");
		}else{
			$result['error'] = 1;
			$result['msg'] = '激活类型错误';
			echo json_encode($result);exit;
		}

		if(!$code_info){
			$result['error'] = 1;
			$result['msg'] = '激活码不存在';
			echo json_encode($result);exit;
		}

		if($code_info['status'] == 2 || $code_info['use_uid'] > 0){
			$result['error'] = 1;
			$result['msg'] = '激活码已经被使用';
			echo json_encode($result);exit;
		}

		$order_info = get_field_by_where("shopping_orders", "*", "order_num='{$order_num}'");
		if(!$order_info){
			$result['error'] = 1;
			$result['msg'] = '订单不存在';
			echo json_encode($result);exit;
		}

		if($order_info['order_status'] == 2){
			$result['error'] = 1;
			$result['msg'] = '该订单已经发放过激活码';
			echo json_encode($result);exit;
		}

		if($order_info['order_status'] != 1){
			$result['error'] = 1;
			$result['msg'] = '该订单尚未支付，无法发放激活码';
			echo json_encode($result);exit;
		}

		$order_item = get_field_by_where("shopping_order_item", "*", "order_id='{$order_info['order_id']}'");
		if(!$order_item){
			$result['error'] = 1;
			$result['msg'] = '订单不存在';
			echo json_encode($result);exit;
		}

		//发放激活码
		$time = time();
		$update_code_data = array("status" => 2, "use_time" => $time, "use_uid" => $order_info['buyer_user_id']);
		if($data_type > 0 && $data_type < 4){
			$use_code_data['course_id'] = $order_item['course_id'];
			$use_code_data['course_type'] = $order_info['order_type'];
			$use_code_data['user_id'] = $order_info['buyer_user_id'];
			$use_code_data['code'] = $code_info['code'];
			$use_code_data['order_num'] = $order_info['order_num'];
			$use_code_data['school_id'] = $order_info['school_id'];
			$use_code_data['add_time'] = $time;
			$use_code_data['effective_time'] = $order_item['effective_time'];
			$this->db->insert("o2o_student_course", $use_code_data);
			if($this->db->insert_id() > 0){
				//修改实训激活码的使用状态
				$this->db->where("code", $code);
				$this->db->update("o2o_school_system_video_course_codes", $update_code_data);
				$this->db->update('shopping_orders', array('order_status' => 2), array('order_id' => $order_info['order_id']));
				$result['error'] = 0;
				$result['msg'] = '激活码发放成功';
				$this->add_terms($order_info['buyer_user_id'], $order_info['order_type'], $order_item['course_id'], $order_item['effective_time'], $order_info['order_id'], $order_info['school_id']);
				echo json_encode($result);exit;
			}else{
				$result['error'] = 1;
				$result['msg'] = '激活码发放失败，请重试';
				echo json_encode($result);exit;
			}
		}elseif ($data_type == 4) {
			$use_code_data['subject_id'] = $order_item['course_id'];
			$use_code_data['uid'] = $order_info['buyer_user_id'];
			$use_code_data['code'] = $code_info['code'];
			$use_code_data['order_num'] = $order_info['order_num'];
			$use_code_data['school_id'] = $order_info['school_id'];
			$use_code_data['use_time'] = $time;
			$use_code_data['effective_time'] = $order_item['effective_time'];
			$this->db->insert("o2o_student_system_topics", $use_code_data);
			if($this->db->insert_id() > 0){
				//修改题库激活码的使用状态
				$this->db->where("code", $code);
				$this->db->update("o2o_school_system_topic_codes", $update_code_data);
				$this->db->update('shopping_orders', array('order_status' => 2), array('order_id' => $order_info['order_id']));
				$result['error'] = 0;
				$result['msg'] = '激活码发放成功';
				$this->add_terms($order_info['buyer_user_id'], $order_info['order_type'], $order_item['course_id'], $order_item['effective_time'], $order_info['order_id'], $order_info['school_id']);
				echo json_encode($result);exit;
			}else{
				$result['error'] = 1;
				$result['msg'] = '激活码发放失败，请重试';
				echo json_encode($result);exit;
			}
		}elseif ($data_type == 5) {
			$use_code_data['sx_id'] = $order_item['course_id'];
			$use_code_data['uid'] = $order_info['buyer_user_id'];
			$use_code_data['code'] = $code_info['code'];
			$use_code_data['order_num'] = $order_info['order_num'];
			$use_code_data['school_id'] = $order_info['school_id'];
			$use_code_data['use_time'] = $time;
			$use_code_data['effective_time'] = $order_item['effective_time'];
			$this->db->insert("o2o_student_system_shixun", $use_code_data);
			if($this->db->insert_id() > 0){
				//修改实训激活码的使用状态
				$this->db->where("code", $code);
				$this->db->update("o2o_school_system_shixun_codes", $update_code_data);
				$this->db->update('shopping_orders', array('order_status' => 2), array('order_id' => $order_info['order_id']));
				$result['error'] = 0;
				$result['msg'] = '激活码发放成功';
				$this->add_terms($order_info['buyer_user_id'], $order_info['order_type'], $order_item['course_id'], $order_item['effective_time'], $order_info['order_id'], $order_info['school_id']);
				echo json_encode($result);exit;
			}else{
				$result['error'] = 1;
				$result['msg'] = '激活码发放失败，请重试';
				echo json_encode($result);exit;
			}
		}
	}
	function add_terms($uid, $order_type, $data_id, $effective_time, $order_no, $school_id){
		//插入有效期表的数据,先查询是否存在有效期数据,如果有就更新，没有就插入
		$time = time();
		$where = "data_type='{$order_type}' AND data_id='{$data_id}' AND uid='{$uid}'";
		$terms_info = get_field_by_where("o2o_student_item_terms", "*", $where);
		$terms_data['school_id'] = $school_id;
		$terms_data['data_type'] = $order_type;
		$terms_data['data_id'] = $data_id;
		$terms_data['uid'] = $uid;
		$terms_data['lose_time'] = 3600 * 24 * $effective_time + $time;
		$terms_data['last_order_id'] = $order_no;
		if($terms_info){
			if($terms_info['lose_time'] > $time){
				$terms_data['lose_time'] = 3600 * 24 * $effective_time + $terms_info['lose_time'];
			}
			$this->db->where("id", $terms_info['id']);
			$this->db->update("o2o_student_item_terms", $terms_data);
		}else{
			$this->db->insert("o2o_student_item_terms", $terms_data);
		}
	}
}